/**Resilience4j+Eureka+RestTemplate/WebClient
 * 1）依赖库：SpringCloud版本为2020.0.0及以上，核心依赖为spring-cloud-starter-circuitbreaker-reactor-resilience4j
 * 2）调用服务的controller（MyWebClient）方法上 @@CircuitBreaker(fallbackMethod="myFallBack")注解之
 * 3）实现降级方法myFallBack，参数和返回值与被注解的方法要完全一致，从而实现被注解方法调用的服务故障时的熔断、降级和隔离
 * 4）重试是@Retry，限流是@RateLimiter，用不同注解来区别，所有Resilience4j属性在配置文件（yml）中配置。
 * 5）纯代码也可以实现上述功能：用CircuitBreakerConfig、RateLimiterConfig、RetryConfig定义相关对象，然后调用一系列相关方法
 */
package com.example.demoresilience4jconsumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.reactive.function.client.WebClient;

@SpringBootApplication
@EnableDiscoveryClient
public class DemoResilience4jConsumerApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoResilience4jConsumerApplication.class, args);
    }

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    @Bean
    @LoadBalanced
    WebClient.Builder webClientBuilder() {
        return WebClient.builder();
    }

    @Bean
    WebClient webClient() {
        return webClientBuilder().build();
    }
}
